package com.winforsys.yellowbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import android.content.Context;


public class FaverateManager {
	
	private static ArrayList<String> mFaverList = new ArrayList<String>(); 
	
	public static void load(Context context) {
		
		try {
			FileInputStream fis = context.openFileInput("file.txt");
			
			byte[] data = new byte[fis.available()];
			
			while (fis.read(data)!= -1) {}
				
			fis.close();
			
			String text = new String(data);
			
			String[] ids = text.split("\r\n");
			
			for (String id : ids){
				if (!id.isEmpty())
					mFaverList.add(id);
			}
			
		}catch (FileNotFoundException ex){
			
			ex.printStackTrace();
		
		}catch (Exception ex){
			
			ex.printStackTrace();
		
		}
	}
	
	public static void save(Context context) {
		
		try{
			FileOutputStream fos = context.openFileOutput("file.txt", Context.MODE_PRIVATE);
			
			for (String id : mFaverList) {
				fos.write((id + "\r\n").getBytes());
			}
			
			fos.close();
			
		} catch(FileNotFoundException ex){
			
			ex.printStackTrace();
			
		} catch(SecurityException ex){
			
			ex.printStackTrace();
			
		} catch(Exception ex){
			
			ex.printStackTrace();
			
		}
	}
	
	public static void remove(String id) {
		if (mFaverList.contains(id)) {
			mFaverList.remove(id);
		}
	}
	
	public static void append (String id) {
		if (!mFaverList.contains(id)) {
			mFaverList.add(id);
		}
	}
	
	public static boolean exists(String id) {
		return mFaverList.contains(id);
	}
	
	public static String[] getAll() {
		return mFaverList.toArray(new String[mFaverList.size()]);
	}
}
